package com.example.useokhttp;

import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Handler;
import android.os.Message;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;

import java.io.IOException;

import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.FormBody;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.RequestBody;
import okhttp3.Response;

public class MainActivity extends AppCompatActivity {
    private Button btn1;
    private Button btn2;
    private Button btn3;
    private Button btn4;
    private TextView textView;
    private ImageView imageView;
    private OkHttpClient okHttpClient;
    private Handler handler;
    private static final String TAG = "MainActivity";
    private EditText editText;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        useFindViewById();

        //异步
        btn1.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String url="http://www.baidu.com";
                Request request = new Request.Builder()
                        .url(url)
                        .build();

                Call call = okHttpClient.newCall(request);
                call.enqueue(new Callback() {
                    @Override
                    public void onFailure(Call call, IOException e) {
                        Log.e(TAG,"onFailure = "+e.toString());
                    }

                    @Override
                    public void onResponse(Call call, Response response) throws IOException {
                       String content = response.body().string();
                        Log.e(TAG,"onResponse = "+content);

                        //显示上在应用上
                        Message message = new Message();
                        message.what = 1001;
                        message.obj = content;
                        handler.sendMessage(message);
                    }
                });
            }
        });

        //同步   需要线程   Thread
        btn2.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                final String imgUrl =  "https://b-ssl.duitang.com/uploads/item/201802/20/20180220100731_JmUCS.jpeg";;
                new Thread(new Runnable() {
                    @Override
                    public void run() {
                        Request request = new Request.Builder()
                                .url(imgUrl)
                                .build();
                        Call call = okHttpClient.newCall(request);
                        try {
                            Response response = call.execute();
                            //图片格式
                            Bitmap bitmap = BitmapFactory.decodeStream(response.body().byteStream());
                            Message message = new Message();
                            message.what = 1002;
                            message.obj = bitmap;
                            handler.sendMessage(message);

                        } catch (IOException e) {
                            e.printStackTrace();
                        }
                    }
                }).start();
            }
        });

        //post手机归属地
        btn3.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String phone = editText.getText().toString();
                String url = "http://ws.webxml.com.cn/WebServices/MobileCodeWS.asmx/getMobileCodeInfo";
                RequestBody requestBody = new FormBody.Builder()
                        .add("mobileCode",phone)
                        .add("userID","")
                        .build();

                Request request = new Request.Builder()
                        .url(url)
                        .post(requestBody)
                        .build();

                Call call = okHttpClient.newCall(request);
                call.enqueue(new Callback() {
                    @Override
                    public void onFailure(Call call, IOException e) {
                        Log.e(TAG,"onFailure = "+e.toString());
                    }

                    @Override
                    public void onResponse(Call call, Response response) throws IOException {
                        String content = response.body().string();
                        Log.e(TAG,"onResponse = "+content);

                        //显示上在应用上
                        Message message = new Message();
                        message.what = 1003;
                        message.obj = content;
                        handler.sendMessage(message);
                    }
                });
            }
        });


        //Get手机归属地
        btn4.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String phone = editText.getText().toString();
                String url ="http://ws.webxml.com.cn/WebServices/MobileCodeWS.asmx/getMobileCodeInfo";
                String url2 = url.concat("?mobileCode=").concat(phone).concat("&userID=");
                Request request = new Request.Builder()
                        .url(url2)
                        .build();
                Call call = okHttpClient.newCall(request);
                call.enqueue(new Callback() {
                    @Override
                    public void onFailure(Call call, IOException e) {
                        Log.e(TAG,"onFailure = "+e.toString());
                    }

                    @Override
                    public void onResponse(Call call, Response response) throws IOException {
                        String content = response.body().string();
                        Log.e(TAG,"onResponse = "+content);

                        //显示上在应用上
                        Message message = new Message();
                        message.what = 1004;
                        message.obj = content;
                        handler.sendMessage(message);
                    }
                });

            }
        });
    }

    //控件
    private void useFindViewById() {
        btn1 = findViewById(R.id.btn1);
        btn2 = findViewById(R.id.btn2);
        btn3 = findViewById(R.id.btn3);
        btn4 = findViewById(R.id.btn4);
        imageView = findViewById(R.id.image);
        textView = findViewById(R.id.text1);
        editText = findViewById(R.id.editText);

        okHttpClient = new OkHttpClient();
        handler = new Handler(new Handler.Callback() {
            @Override
            public boolean handleMessage(Message msg) {
                if (msg.what==1001){
                    String msgContent = (String) msg.obj;
                    textView.setText(msgContent);
                }else if (msg.what==1002){
                    imageView.setImageBitmap((Bitmap) msg.obj);
                }else if (msg.what==1003){
                    String postContent = (String) msg.obj;
                    textView.setText(postContent);
                }else if (msg.what==1004){
                    String getContent = (String) msg.obj;
                    textView.setText(getContent);
                }
                return false;
            }
        });
    }


}
